<template>
    <div class='index'>
        <div v-if="showbox">
            <div class="individual" @click="login">
                <van-image round width="5rem" height="5rem"
                    src="https://img.51miz.com/Element/00/88/82/42/c21e019b_E888242_0f2360ce.png" />
                <div class="ind-text">
                    <h3>点击登录</h3>
                </div>
            </div>
            <div class="myList">
                <van-grid :column-num="4" :border="false">
                    <van-grid-item v-for="v in icons" :key="v.title" :icon="v.icon" :text="v.title" />
                </van-grid>
                <div class="downloadList"><span><van-icon name="down" /> 下载列表</span><span>13个资源下载失败</span></div>
            </div>
            <div class="collect">
                <p class="header"><span>创建歌单</span><span><van-icon name="plus" /><van-icon name="ellipsis" /></span></p>
                <div class="sheet">
                    <!-- <div class="sheetImg">
                        <van-image radius="15px" width="4rem" height="4rem" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
                        <div class="sheetText">
                            <h3>1</h3>
                            <p>49首,已下载10首</p>
                        </div>
                    </div>
                    <van-icon name="ellipsis" /> -->
                </div>
            </div>
            <van-popup v-model="show">
                <van-image width="260" height="260" radius="10" fit="cover" :src="qrimg" />
            </van-popup>
        </div>
        <div v-if="!showbox">
            <!-- -->
            <keep-alive>
                <success :userData="userData" :Lv="Lv" :followeds="Number(followeds.length)"
                    :follows="Number(follows.length)" :Playlist="Playlist">
                </success>
            </keep-alive>
        </div>
    </div>
</template>

<script>
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex';// 导入vuex
import { getQrKey, getQr, checkQr, getStatus, getLv, getfolloweds, getUserDetail, getSubcount, getfollows, getAccount, getPlaylist } from "@/api"// 接口
import success from './success'

export default {
    name: 'my-index',
    data() {
        return {
            icons: [
                {
                    icon: require('@/assets/a1.png'),
                    title: '最近播放',
                },
                {
                    icon: require('@/assets/a2.png'),
                    title: '本地下载',
                },
                {
                    icon: require('@/assets/a3.png'),
                    title: '云盘',
                },
                {
                    icon: require('@/assets/a4.png'),
                    title: '已购',
                },
                {
                    icon: require('@/assets/a5.png'),
                    title: '我的好友',
                },
                {
                    icon: require('@/assets/a6.png'),
                    title: '收藏和赞',
                },
                {
                    icon: require('@/assets/a7.png'),
                    title: '我的播客',
                },
                {
                    icon: require('@/assets/a8.png'),
                    title: '交友',
                }
            ],
            userData: {},
            show: false,
            showbox: true,
            qrimg: '',
            status: false,
            Lv: 0,
            followeds: 0,
            follows: 0,
            Playlist: []
        }
    },
    computed: {
        ...mapGetters(['getMsg', 'xstatus']),
        ...mapState(['msg']),
    },
    components: {
        success
    },

    created() { },
    mounted() {

    },
    methods: {
        ...mapActions(['changeMsg', 'toggleCookie', 'toggleAccount']),
        ...mapMutations(['CHANGEMSG']),
        async login() {
            var { data } = await getQrKey()
            let key = data.data.unikey
            var { data } = await getQr(key)
            let qrimg = data.data.qrimg
            this.qrimg = qrimg
            this.show = true
            let that = this
            let timer = setInterval(async () => {
                var { data } = await checkQr(key)
                if (data.code == 800) {
                    this.getQrCodeImg()
                    clearInterval(timer)
                } else if (data.code == 803) {
                    this.toggleCookie(data.cookie)
                    localStorage.setItem('myLogin', data.cookie)
                    var { data } = await getStatus(data.cookie)
                    this.toggleAccount({
                        account: data.account,
                        profile: data.profile
                    })
                    this.$emit('update', false);
                    that.user = data.profile
                    that.show = false
                    that.showbox = false
                    clearInterval(timer)
                }
            }, 2000)
        },
        async getQrCodeImg() {
            var { data } = await getQrKey()
            let key = data.data.unikey
            var { data } = await getQr(key)
            let qrimg = data.data.qrimg
            this.qrimg = qrimg
            return key
        },
    },
    async beforeMount() {
        if (this.xstatus) {
            this.toggleCookie(this.xstatus)
            let { data } = await getLv(this.xstatus)
            this.Lv = data.data.level
            this.showbox = !this.xstatus
            let userData = await getStatus(this.xstatus)
            userData = userData.data.data
            // console.log(userData);
            this.userData = userData
            // console.log(this.userData);
            // 用户信息
            // let a = await getUserDetail(this.status, this.userData.profile.userId)
            // let a = await getSubcount(this.status)
            let followeds = await getfolloweds(this.xstatus, this.userData.profile.userId)
            // console.log(followeds.data);
            this.followeds = followeds.data.followeds
            let follows = await getfollows(this.xstatus, this.userData.profile.userId)
            // console.log(follows.data);
            this.follows = follows.data.follow
            // let account = await getAccount(this.status)
            // console.log(account);
            let Playlist = await getPlaylist(this.xstatus, this.userData.profile.userId)
            this.Playlist = Playlist.data.playlist
        }
    },
}
</script>
<style scoped>
* {
    margin: 0;
    padding: 0;
}

.index {
    background: #eee;
    padding-bottom: 80px;
}

.individual {
    position: relative;
    top: 5rem;
    background: #fff;
    width: 80%;
    margin: 0 auto;
    text-align: center;
    height: 160px;
    border-radius: 20px;
}

.individual>.van-image {
    position: absolute;
    top: -2.5rem;
    left: 110px;
}

.ind-text {
    padding-top: 60px;
}

.ind-text p {
    margin-top: 15px;
    display: flex;
    justify-content: center;
    line-height: 15px;
}

.ind-text span {
    padding: 0 5px;
}

.myList {
    width: calc(80% - 20px);
    margin: 100px auto 0;
    background: #fff;
    padding: 10px;
    border-radius: 20px;
}

.downloadList {
    display: flex;
    justify-content: space-between;
    margin: 0 auto;
    font-size: 10px;
    color: #777;
}

::v-deep .van-grid-item__content {
    padding: 8px 4px;
}

.collect {
    background: #fff;
    margin: 0 auto;
    padding: 10px 5px;
    width: calc(80% - 20px);
    border-radius: 20px;
    margin-top: 40px;
}

.header {
    display: flex;
    justify-content: space-between;
    padding: 10px;
}

/* .sheet {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    padding: 0 10px;
    align-self: center;
} */

.sheetImg {
    display: flex;
    justify-content: space-between;
}

/* .sheetText {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 10px;
} */

.sheet>.van-icon {
    line-height: 64px;
}

.sheet>h3 {
    text-align: center;
    color: #777;
}
</style>
